//
//  CustomRemarkView.m
//  ReceiptDelivery
//
//  Created by 吕晶 on 2020/7/30.
//  Copyright © 2020 吕晶. All rights reserved.
//

#import "CustomRemarkView.h"

@interface CustomRemarkView ()<UITextViewDelegate>

@end

@implementation CustomRemarkView

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        self.textLength = 80;
        self.countLab = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 13)];
        self.countLab.textColor = HexColor(0xBDBED9);
        self.countLab.font = [UIFont systemFontOfSize:12];
        [self addSubview:self.countLab];
        self.textView = [[UITextView alloc] initWithFrame:CGRectMake(0, 20, frame.size.width, 80)];
        self.textView.delegate = self;
        [self addSubview:self.textView];
    }
    return self;
}

-(void)setViewText:(NSString *)viewText {
    _viewText = viewText;
    self.textView.text = viewText;
    [self.textView.delegate textViewDidChange:self.textView];
}

-(void)textViewDidChange:(UITextView *)textView {
    if (self.textView.text.length == 0) {
        _placeholderLab.hidden = NO;
    }else{
        _placeholderLab.hidden = YES;
    }
    NSString *toBeString = self.textView.text;
    NSString *primaryLanguageStr = self.textInputMode.primaryLanguage; // 键盘输入模式
    if ([primaryLanguageStr isEqualToString:@"zh-Hans"]) {
        UITextRange *selectedRange = [self.textView markedTextRange];
        //获取高亮部分
        UITextPosition *position = [self.textView positionFromPosition:selectedRange.start offset:0];
        //没有高亮选择的字，则对已输入的文字进行字数统计和限制
        if (!position) {
            if (toBeString.length > _textLength) {
//                if (_interception) {
                    self.textView.text = [toBeString substringToIndex:_textLength];
//                }else {
//
//                }
                [self changeCountLab];
            }else {
                [self changeCountLab];
            }
        }else {
            //有高亮选择的字符串，则暂不对文字进行统计和限制
        }
    }else {
        //中文输入法以外的直接对其统计限制即可，不考虑其他语种情况
        if (toBeString.length > _textLength) {
//            if (_interception) {
                self.textView.text = [toBeString substringToIndex:_textLength];
//            }else {
//
//            }
            [self changeCountLab];
        }else {
            [self changeCountLab];
        }
    }
    
}

- (void)changeCountLab {
    NSString *changeStr = [NSString stringWithFormat:@"%ld/%ld",self.textView.text.length,_textLength];
//    if (!_interception && (self.text.length > _textLength)) {
//        NSMutableAttributedString *attributedStr = [[NSMutableAttributedString alloc] initWithString:changeStr];
//        [attributedStr addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(0, changeStr.length - [NSString stringWithFormat:@"%ld",_textLength].length)];
//        _promptLab.attributedText = attributedStr;
//    }else {
        _countLab.text = changeStr;
//    }
//    [_promptLab sizeToFit];
//    _promptLab.frame = CGRectMake(CGRectGetMaxX(self.frame)-CGRectGetWidth(_promptLab.frame)-_promptFrameMaxX, CGRectGetMaxY(self.frame)-CGRectGetHeight(_promptLab.frame)-_promptFrameMaxY, CGRectGetWidth(_promptLab.frame), CGRectGetHeight(_promptLab.frame));
}

@end
